Page({
  data: {
    saleOrder:{
      originalMoney:0,//订单原价
      receivableMoney:0,//订单应收
      deliveryType:2,//订单类型 1:堂食，2:外卖，
      deliveryMoney:0,//配送费
      deliveryOrder:{},
      orderItems:[]
    },
    couponList:[],
    selectedCoupon: {
      name: '新用户专享优惠券',
      desc: '满¥500减¥50',
      value: '50.00'
    },
    deliveryAddress: {},
    totalPrice: 0
  },

  onLoad: function() {
    this.calculateTotalPrice();
    let shopCart = getApp().globalData.shopCart;
    this.data.saleOrder.originalMoney=shopCart.cartMoney
    for (let goodsId in shopCart.goodsList) {
      let goodsEntity = shopCart.goodsList[goodsId]
      let goodsIdNames = goodsEntity.goodsIdNames;
      for (let goodsIdName in goodsIdNames) {
        let goodsSpecName = goodsIdName.split("_")
        let goodsNameEntity =  goodsIdNames[goodsIdName];
        let orderItem = {
          goodsId:goodsId,
          goodsName:goodsSpecName[0],
          goodsNum:goodsNameEntity.goodsNameNum,
          skuName:goodsSpecName[1]?goodsSpecName[1]:"",
          price:goodsNameEntity.goodsNamePrice
        }
        this.data.saleOrder.orderItems.push(orderItem)
      }
    }
    this.setData({saleOrder:this.data.saleOrder})
    this.initData()
  },
  initData(){
    //初始化数据
    this.setData({
      'saleOrder.deliveryOrder': getApp().globalData.defaultAddress
    })
    //订阅事件
    const _this = this
    getApp().eventBus.on("defaultAddressUpdate", (defaultAddress) => {
      _this.setData({
        'saleOrder.deliveryOrder': defaultAddress
      })
    })
  },
  // 选择配送方式
  selectDelivery: function(e) {
    const type = e.currentTarget.dataset.type;
    this.setData({
      'saleOrder.deliveryType': type,
      'saleOrder.deliveryMoney': type === '2' ? 8.00 : 0
    });
    this.calculateTotalPrice();
  },

  // 计算总价
  calculateTotalPrice: function() {
    let totalProductPrice = 0;
    // this.data.products.forEach(product => {
    //   totalProductPrice += parseFloat(product.price) * product.quantity;
    // });
    
    // const couponDiscount = this.data.selectedCoupon ? parseFloat(this.data.selectedCoupon.value) : 0;
    // const totalPrice = totalProductPrice + this.data.shippingFee - couponDiscount;
    
    // this.setData({
    //   totalProductPrice: totalProductPrice.toFixed(2),
    //   totalPrice: totalPrice.toFixed(2)
    // });
  },

  //
  changeDeliveryAddress:function(){
    wx.navigateTo({
      url: '/pages/user-address/index?modalType=2',
    })
  },

  // 选择优惠券
  selectCoupon: function() {
    wx.showToast({
      title: '选择优惠券功能',
      icon: 'none'
    });
  },
  // 提交订单
  submitOrder: function() {
    wx.showModal({
      title: '确认订单',
      content: `确认提交订单？总金额：¥${this.data.totalPrice}`,
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '订单提交成功',
            icon: 'success'
          });
          // 模拟跳转到订单详情页
          setTimeout(() => {
            wx.navigateTo({
              url: '/pages/pickup/index?modalType=1',
            })
          }, 1500);
        }
      }
    });
  },
  pickup(){
    wx.navigateTo({
      url: '/pages/pickup/index?modalType=1',
    })
  }
});